package character;

import action.Action;
import board.BoardUtil;
import board.PatternType;
import com.jme3.bullet.collision.shapes.CollisionShape;
import game.DataStore;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import misc.Vector3i;

public class CharacterRoamControl extends MyCharacterControl {
    protected Set<Vector3i> validMoveLocations = new HashSet<Vector3i>();
    protected float timeSinceLastTick = 0f;
    
    @Deprecated
    protected CharacterRoamControl() {}
    
    public CharacterRoamControl(CollisionShape collisionShape) {
        super(collisionShape);
    }
    
    @Override
    public boolean tick() {
        boolean tickSuccessful = super.tick();
        if(!queuedActions.isEmpty()) { //remove all completed actions from the front of the queue
            Iterator<Action> iter = queuedActions.iterator();
            while(iter.hasNext()) {
                Action action = iter.next();
                if(action.isCompleted()) {
                    queuedActions.remove(action);
		    repopulateValidMoveLocations();
                } else {
                    return tickSuccessful;
                }
            }
        }
        return tickSuccessful;
    }
    
    @Override
    public void update(float tpf) {
        timeSinceLastTick += tpf;
        if(timeSinceLastTick >= DataStore.TIME_PER_TICK) {
            tick();
            timeSinceLastTick = 0f;
        }
        super.update(tpf);
    }
    
    @Override
    public Collection<Vector3i> getValidMoveLocations() {
        if(validMoveLocations.isEmpty())
            repopulateValidMoveLocations();
        return validMoveLocations;
    }
    
    @Override
    public void repopulateValidMoveLocations() {
	validMoveLocations.clear();
	for(Vector3i occupiedLocation : myCharacter.getOccupiedLocations())
	    validMoveLocations.addAll(BoardUtil.getPatternLocations(PatternType.SQUARE, 1, false, occupiedLocation));
	validMoveLocations.removeAll(myCharacter.getOccupiedLocations());
    }
}
